package com.mg.plugin.agrometeorology.model;

import com.mg.model.Model;
import com.mg.model.Param;
import com.mg.model.Port;
import com.mg.plugin.agrometeorology.common.AssistStationData;
import com.mg.plugin.agrometeorology.common.ChillingDamageCalculator;
import com.mg.plugin.agrometeorology.entity.StationData;
import com.mg.pluginexample2.ModuleBase;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 作物水分亏缺指数
 *
 * @author xiaolu
 */
public class CalculateChillingDamageModule extends ModuleBase {

    public CalculateChillingDamageModule(int nID) {
        super(nID);
        m_alInputPort.add(new Port(this, Port.Type.Unknown));
        m_alOutputPort.add(new Port(this, Port.Type.Unknown));
    }

    @Override
    public String GetGroupName() {
        return "低温冷害";
    }

    @Override
    public String GetName() {
        return "玉米/水稻低温冷害";
    }

    @Override
    public String GetDescription() {
        return "玉米/水稻低温冷害";
    }

    @Override
    public boolean OnAttach(Port portFrom, Port portTo) {
        try {
            int i = FindPort(portFrom, false);
            if (i != -1) {
                return true;
            }
            i = FindPort(portTo, true);
            if (i == -1) {
                return false;
            }

            ArrayList<Param> alParam = new ArrayList<Param>();
            portFrom.GetModule().GetOutputParam(portFrom, alParam);

            if (alParam.size() != 1) {
                return false;
            }

            if (alParam.get(0).m_objValue != null && !(alParam.get(0).m_objValue instanceof ArrayList)) {
                m_model.OutputLog(Model.LogLevel.Error, "No ArrayList");
                return false;
            }
            if (i == 0) {
                m_model.OutputLog(Model.LogLevel.Info, "m_inputForecastList" + alParam.get(0).m_objValue.getClass());
                m_inputForecastList = (List<String>) alParam.get(0).m_objValue;
            }

            portTo.SetType(portFrom.GetType());
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean OnDetach(Port portFrom, Port portTo) {
        if (!super.OnDetach(portFrom, portTo)) {
            return false;
        }
        try {
            int i = FindPort(portTo, true);
            if (i == -1)
                return true;
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    //用于 参数->XML 等
    @Override
    public int GetParam(ArrayList<Param> alParam) {
        try {
            super.GetParam(alParam);
            String selectData = "Rice|Corn";
            alParam.add(new Param("StationId", m_strStationId, "站点ID", "StationId", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("Date", m_strDate, "数据时间", "Date", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("Temp", m_strTemp, "站点温度", "Temp", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("OldTemp", m_strOldTemp, "站点常年温度", "OldTemp", "参数匹配", Param.EditType.Default));
            alParam.add(new Param("CropType", m_strCropType, "作物种类", "CropType", "作物类型", Param.EditType.Select, selectData));

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return alParam.size();
    }

    @Override
    public boolean OnParamChanged(ArrayList<Param> alParam, int nIndex, Object objValue) {
        return super.OnParamChanged(alParam, nIndex, objValue);
    }

    //用于 XML->参数 等
    @Override
    public int SetParam(final ArrayList<Param> alParam) {
        int i = super.SetParam(alParam);
        try {
            m_strStationId = (String) alParam.get(i++).m_objValue;
            m_strDate = (String) alParam.get(i++).m_objValue;
            m_strTemp = (String) alParam.get(i++).m_objValue;
            m_strOldTemp = (String) alParam.get(i++).m_objValue;
            m_strCropType = (String) alParam.get(i++).m_objValue;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return i;
    }

    @Override
    public int GetOutputParam(Port port, ArrayList<Param> alParam) {
        int i = FindPort(port, false);
        if (i == -1) {
            System.out.println("Post Is Null");
            return 0;
        }

        alParam.add(new Param("ResultString", m_listCSV));
        return alParam.size();
    }

    @Override
    @SuppressWarnings("static-access")
    public boolean Execute() {

        m_listCSV.clear();


        Map<String, String> headerIndex = new HashMap<String, String>();
        headerIndex.put("StationId", m_strStationId);
        headerIndex.put("Date", m_strDate);
        headerIndex.put("Temp", m_strTemp);
        headerIndex.put("OldTemp", m_strOldTemp);

        Date now = m_model.m_dateDataStart;

        Map<String, List<StationData>> newStationDataMap = AssistStationData.mapByStationId(headerIndex, m_inputForecastList);
        try {
            if (RICE.equals(m_strCropType)) {
                m_listCSV.addAll(ChillingDamageCalculator.calcRiceChillingDamage(newStationDataMap, now));
            }
            if (CORN.equals(m_strCropType)) {
                m_listCSV.addAll(ChillingDamageCalculator.calcCornChillingDamage(newStationDataMap, now));
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }

        OnParamChanged();
        return true;
    }

    private List<String> m_inputForecastList = new ArrayList<String>();
    private List<String> m_listCSV = new ArrayList<String>();

    private final static String RICE = "Rice";
    private final static String CORN = "Corn";
    private String m_strStationId;
    private String m_strDate;
    private String m_strTemp;
    private String m_strOldTemp;
    private String m_strCropType;

}
